<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑菜单' : '添加菜单'}}</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单链接">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.parentId" placeholder="请选择上级菜单">
              <el-option label="无上级菜单" :value="-1"></el-option>
            <el-option v-for="menu in parentMenu" :key="menu.id" :label="menu.name" :value="menu.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="form.shown">
            <el-radio :label="true"></el-radio>
            <el-radio :label="false"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="form.orderNum"
            :min="0"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit" type="success">提交</el-button>
          <el-button v-if="!isEdit" @click="reset" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { createOrUpdateMenu, getEditMenuInfo } from '@/apis/menu'
export default Vue.extend({
  name: 'menu-create',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: 0,
        description: '',
        shown: true
      },
      parentMenu: []
    }
  },
  created() {
    this.getParentMenu()
  },
  methods: {
    async getParentMenu() {
      const { data } = await getEditMenuInfo(this.$route.params.id || -1)
      if (data.code === '000000') {
        if (this.isEdit) this.form = data.data.menuInfo
        this.parentMenu = data.data.parentMenuList
      }
    },
    async onSubmit() {
      const { data } = await createOrUpdateMenu(this.form)
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '修改菜单成功！' : '创建菜单成功！')
        this.$router.back()
      }
    },
    reset() {
      this.form.parentId = -1
      this.form.name = ''
      this.form.href = ''
      this.form.icon = ''
      this.form.orderNum = 0
      this.form.description = ''
      this.form.shown = true
    }
  }
})
</script>
<style lang="scss" scoped>
</style>
